package qianfeng.com.fan_shu_xiao_bao.view;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import qianfeng.com.fan_shu_xiao_bao.R;
import qianfeng.com.fan_shu_xiao_bao.view.base.BaseFragment;
import qianfeng.com.fan_shu_xiao_bao.view.base.Base_RecyclerView_Adapter;

/**
 * 内容为顶部是可展开的标签栏,下册是一个Fragment
 * 因为用的地方比较多而单独摘出来
 * <p>
 * 设计思路:
 * 该类具有功能:
 * 显示顶部的标签,可以展开标签,可以关闭标签
 * <p>
 * 使用该类需要实现的功能:
 * 1、使用getContentView()设置内容视图。
 * 2、使用标签点击监听对内容视图做出相应的改变。
 */
public abstract class TopTabFragment extends BaseFragment implements Base_RecyclerView_Adapter.OnItemClickListener<String, TopTab_RecyclerView_Adapter.ViewHolder> {

    @InjectView(R.id.fragment_topTab_RecyclerView)
    RecyclerView topTabRecyclerView;
    @InjectView(R.id.fragment_topTab_ImageView)
    ImageView topTabImageView;
    @InjectView(R.id.fragment_topTab_content_View)
    RecyclerView contentView;
    private TopTab_RecyclerView_Adapter topTabAdapter;
    private List<String> tabs;

    public List<String> getTabs() {
        return tabs;
    }

    public void setTabs(List<String> tabs) {
        this.tabs = tabs;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_top_tab, container, false);
        ButterKnife.inject(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        tabs = initTabs(tabs);
        initView();
    }

    private void initView() {
        topTabRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        //使用上一次的topTabAdapter来保存曾经的选中状态
        if (topTabAdapter == null) {
            topTabAdapter = new TopTab_RecyclerView_Adapter(getContext(), tabs);
            topTabAdapter.setOnItemClickListener(this);
        }
        topTabRecyclerView.setAdapter(topTabAdapter);

        setContentView(contentView);
    }

    //点击 展开/关闭 标签栏
    @OnClick(R.id.fragment_topTab_ImageView)
    public void onClick() {
    }

    //点击标签回调
    @Override
    public void onItemClick(RecyclerView parent, View view, TopTab_RecyclerView_Adapter.ViewHolder holder, int position, String data) {
        //完全显示点击项
        LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
        if (position == layoutManager.findFirstVisibleItemPosition()) {
            layoutManager.scrollToPositionWithOffset(position, 0);
        } else if (position == layoutManager.findLastVisibleItemPosition()) {
            layoutManager.scrollToPosition(position);
        }

        onTabClickListener(data, position);
    }

    /**
     * 实现该方法以初始化标签数据
     *
     * @param tabs - 初始值是null
     * @return - 使用返回值赋值
     */
    protected abstract List<String> initTabs(List<String> tabs);

    /**
     * 实现该方法以设置contentView
     *
     * @param contentView - 显示内容的RecyclerView
     */
    public abstract void setContentView(RecyclerView contentView);

    /**
     * 实现此方法以接受点击事件
     *
     * @param Name     - 点击的标签的名字
     * @param position - 点击的标签的位置
     */
    public abstract void onTabClickListener(String Name, int position);
}
